/***************************************************************************
 *  Filename: mastercommunicator.cpp
 *  Class: mastercommunicator
 *
 *  Copyright 2012  Xander van Kooten
 *  <xandervk@gmail.com>
 *
 *  Copyright 2012  Mario Henrique Voorsluys
 *  <forcaeluz@gmail.com>
 *
 ****************************************************************************/

/*
 *
 * This file is part of Parsax-Remote-Controller.
 *
 *    Parsax-Remote-Controller is free software: you can redistribute it
 *    and/or modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    Parsax-Remote-Controller is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Parsax-Remote-Controller.
 *    If not, see <http://www.gnu.org/licenses/>.
 */

#include "mastercommunicator.h"
#include <QDebug>

MasterCommunicator::MasterCommunicator(QObject *parent):QObject(parent){
    tcpSocket = new QTcpSocket(this);
    connect(tcpSocket, SIGNAL(readyRead()), SLOT(receiveMessage()));
}

MasterCommunicator::~MasterCommunicator(){
    if(tcpSocket->isOpen()){
        closeConnection();
    }
}

void MasterCommunicator::connectToSlave(QString address, int port){
    tcpSocket->connectToHost(QHostAddress(address), port);
    qDebug() << "[MasterCommunicator] Connecting to: " << address << ":"<<port;
    if(tcpSocket->waitForConnected()){
        qDebug() << "[MasterCommunicator] Connection established";
        connect(tcpSocket, SIGNAL(disconnected()), SLOT(handleDisconnect()));
        emit connectionEstablished();
    }else{
        qWarning() << "[MasterCommunicator] Not able to establish connection";
        qWarning() << "[MasterCommunicator] Error string: " << tcpSocket->errorString();
    }
}
void MasterCommunicator::closeConnection(){
    tcpSocket->close();
}

void MasterCommunicator::sendData(QByteArray data){
    qDebug() << "[MasterCommunicator] Resquested to send " << data;
    if(tcpSocket->isOpen()){
        data.append("\r\n");
        tcpSocket->write(data);
        tcpSocket->flush();
    }else{
        qWarning() << "[MasterCommunicator] No connection open";
    }

}

bool MasterCommunicator::isConnected(){
    return tcpSocket->isOpen();
}

void MasterCommunicator::receiveMessage(){
    QString tmp = tcpSocket->readLine();
    while(!tmp.isEmpty()){
        qDebug() << "[MasterCommunicator] Message received:" << tmp;
        tmp.replace('\n', "");
        tmp.replace("\r", "");
        if(tmp.startsWith("#") && tmp.endsWith("##")){
            qDebug() << "[MasterCommunicator] Valid message received";
            emit messageReceived(tmp);
        }
        tmp = tcpSocket->readLine();
    }
}

void MasterCommunicator::handleDisconnect(){
    qWarning() << "[MasterCommunicator] Connection with slave lost";
    emit connectionLost();
}
